package server.content.skills.wc;

import java.util.LinkedList;
import java.util.List;

import core.util.Misc;
import server.Server;
import server.game.players.Client;

public class MiracleWC {

	private final Client c;
	private boolean isCutting;
	private Trees myTreeE;
	private GlobalTree myTreeG;
	private int myTreeX = -1, myTreeY = -1, chopAnim = -1;
	public static final List<GlobalTree> undeads = new LinkedList<GlobalTree>();

	public MiracleWC(final Client c) {
		this.c = c;
	}

	public void cutTree(final int tree_id, final int x, final int y) {
		final Trees tree = Trees.getTreeForId(tree_id);
		if (tree == null || isCutting()) {
			resetWC();
			return;
		}
		if (getMyAxe() < 0) {
			c.sendMessage("You need an axe to cut a tree.");
			return;
		}
		if (Axes.getAxe(getMyAxe()).getLvlReq() > c.playerLevel[8]) {
			c.sendMessage("You don't have a high enough woodcutting level to use this axe.");
			resetWC();
			return;
		}
		if (c.getItems().freeSlots() < 1) {
			c.sendMessage("You're inventory is too full to carry more logs.");
			resetWC();
			return;
		}
		if (!c.goodDistance(c.getX(), c.getY(), c.objectX, c.objectY, 3)) {
			resetWC();
			return;
		}
		if (!Server.gth.treeExists(x, y)) {
			Server.gth.registerTree(tree_id, x, y);
		}
		if (Server.gth.getTree(x, y).isDead()) {
			// c.sendMessage("This tree contains no more logs.");
			Server.objectHandler.createAnObject(c, 1341, myTreeX, myTreeY);
			resetWC();
			return;
		}
		if (tree.getLvlReq() <= c.playerLevel[8]) {
			setTreeE(tree);
			setTreeG(Server.gth.getTree(x, y));
			chopAnim = Axes.getAxe(getMyAxe()).getAnim();
			myTreeX = x;
			myTreeY = y;
			if (myTreeG != null) {
				c.turnPlayerTo(c.objectX, c.objectY);
				getClient().sendMessage("You swing your axe at the tree.");
				setCutting(true);
			}
		} else {
			c.sendMessage("You need a woodcutting level of " + tree.getLvlReq()
					+ " to cut this tree.");
		}
	}

	public void processWC() {
		for (final GlobalTree tree : undeads) {
			if (tree == null)
				continue;
			Server.objectHandler.createAnObject(c, tree.getId(), tree.getX(),
					tree.getY());
			undeads.remove(tree);
		}
		if (isCutting()) {
			if (!myTreeG.isDead()) {
				if (c.getItems().freeSlots() < 1) {
					c.sendMessage("You're inventory is too full to carry more logs.");
					resetWC();
					return;
				}
				c.startAnimation(chopAnim);
				c.getPA().requestUpdates();
				final int rand = Misc.random(100);
				final int chance = (getChanceForLevel() + getAxeSpeed())
						- getTreeE().getDiff();
				if (rand <= chance) {
					c.getItems().addItem(getTreeE().getLogId(), 1);
					getTreeG().setCurrLogs(getTreeG().getCurrLogs() - 1);
					c.sendMessage("You recieve some "
							+ c.getItems().getItemName(getTreeE().getLogId())
							+ ".");
					c.getPA().addSkillXP(getTreeE().getXP(), 8);
					c.treeSpirit.initRandom();
				}
			} else {
				Server.objectHandler.createAnObject(c, 1341, myTreeX, myTreeY);
				resetWC();
			}
		}
	}

	public Trees getTreeE() {
		return myTreeE;
	}

	public GlobalTree getTreeG() {
		return myTreeG;
	}

	public void setTreeE(final Trees myTreeE) {
		this.myTreeE = myTreeE;
	}

	public void setTreeG(final GlobalTree myTreeG) {
		this.myTreeG = myTreeG;
	}

	public void resetWC() {
		chopAnim = -1;
		c.startAnimation(65535);
		c.getPA().requestUpdates();
		myTreeG = null;
		myTreeE = null;
		myTreeX = myTreeY = -1;
		setCutting(false);
	}

	public void setCutting(final boolean isCutting) {
		this.isCutting = isCutting;
	}

	public boolean isCutting() {
		return isCutting;
	}

	public Client getClient() {
		return c;
	}

	private final int AXES[] = { 1351, 1353, 1355, 1357, 1359 };

	public int getAxeSpeed() {
		switch (getMyAxe()) {
		case 1351:
			return 2;
		case 1353:
			return 2;
		case 1355:
			return 5;
		case 1357:
			return 7;
		case 1359:
			return 9;
		}
		return 0;
	}

	public int getMyAxe() {
		for (int i = 0; i < AXES.length; i++) {
			if (c.getItems().playerHasItem(AXES[i]))
				return AXES[i];
			if (c.playerEquipment[c.playerWeapon] == AXES[i])
				return AXES[i];
		}
		return -1;
	}

	public int getChanceForLevel() {
		final int lvl = c.playerLevel[10];
		if (lvl > 1 && lvl < 10) {
			return 7;
		} else if (lvl > 9 && lvl < 20) {
			return 15;
		} else if (lvl > 19 && lvl < 39) {
			return 28;
		} else if (lvl > 39 && lvl < 51) {
			return 35;
		} else if (lvl > 50 && lvl < 61) {
			return 42;
		} else if (lvl > 60 && lvl < 71) {
			return 46;
		} else if (lvl > 70 && lvl < 81) {
			return 52;
		} else if (lvl > 80 && lvl < 99) {
			return 56;
		}
		return 10;
	}

	public boolean hasAxe() {
		for (int i = 0; i < AXES.length; i++) {
			if (c.getItems().playerHasItem(AXES[i]))
				return true;
			if (c.playerEquipment[c.playerWeapon] == AXES[i])
				return true;
		}
		return true;
	}

}
